package com.lcf.framework.model;

import com.lcf.framework.exception.ExceptionEnum;
import com.lcf.framework.exception.SysException;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author lcf
 */
@Data
@NoArgsConstructor
public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    public R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <E> R<E> result(int code, E data, String msg) {
        return new R<>(code, data, msg);
    }

    public static <E> R<E> success() {
        return new R<>(ExceptionEnum.SUCCESS.getCode(), null, ExceptionEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> success(T data) {
        return new R<>(ExceptionEnum.SUCCESS.getCode(), data, ExceptionEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> success(T data, String msg) {
        return new R<>(ExceptionEnum.SUCCESS.getCode(), data, msg);
    }

    public static R<Void> fail() {
        return new R<>(ExceptionEnum.ERROR.getCode(), null, ExceptionEnum.ERROR.getMsg());
    }

    public static R<Void> fail(String msg) {
        return new R<>(ExceptionEnum.ERROR.getCode(), null, msg);
    }

    public static R<Void> fail(SysException msg) {
        return new R<>(ExceptionEnum.ERROR.getCode(), null, msg.getMessage());
    }

    public static R<Void> fail(ExceptionEnum msg) {
        return new R<>(msg.getCode(), null, msg.getMsg());
    }
}
